<?php
// anti direct-access
if (!defined('MEKARSARI_WEBGIS'))
	exit('Direct access to this script is prohibited.');

function getSubBagian($bagian='kebun'){
	$subbagian = NULL;
	
	if(isset($_GET['subbagian']))
		$subbagian = trim($_GET['subbagian']);	
	
	return $subbagian;
}

function getBagian(){
    $bagian = NULL;
    
	if(isset($_GET['bagian']))
		$bagian = trim($_GET['bagian']);
	
	return $bagian;
}

function debug($var){
	echo '<pre>';print_r($var);echo '</pre>';
}

// get browser..
// by imamiscoo
function getBrowser(){
	$browser = array (
	  "MSIE",            // parent
	  "OPERA",
	  "MOZILLA",         // parent
	  "NETSCAPE",
	  "FIREFOX",
	  "SAFARI"
	);

	$info['agent'] = "OTHER";
	
	foreach ($browser as $parent) {		
		$s = strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), $parent);
		$f = $s + strlen($parent);
		
		$version = substr($_SERVER['HTTP_USER_AGENT'], $f, 8);
		$version = preg_replace('/[^0-9,.]/','',$version);

		if ( $s !== FALSE ) {
			$info['agent'] = $parent;
			$info['version'] = $version;
		}
		
	}
	
	return $info;
}

function makeTitle($str){
	$out = preg_replace('/_/', ' ', $str);
	
	return ucwords($out);
}

function encodeQuery($q){
	// query -> base64 -> decode URL
	return urlencode(base64_encode($q));
}

function decodeQuery($q){
	return base64_decode(urldecode($q));
}

function quote_smart($value){
   /*
   // Stripslashes
   if (get_magic_quotes_gpc()) {
       $value = stripslashes($value);
   }
   // Quote if not integer
   if (!is_numeric($value)) {
       $value = '"' . mysql_real_escape_string($value) . '"';
   }
   
   */
   return $value;
}

function refresh($msg, $bagian, $subbagian){
	echo '<script>alert("'.$msg.'");</script>';
	echo '<script>window.location="index.php?bagian='.$bagian.'&subbagian='.$subbagian.'&action=entry";</script>';
}

function isValidUser($subbagian){
    if(isLogged()){
        $role = $_SESSION['role'];
        if( in_array($subbagian, array_keys($_SESSION['role'])))
            return true;
        else // selainnya
            return false;	
    }
    else{
        return false;
    }
}

function isLogged(){
    return isset($_SESSION['role']) && count($_SESSION['role']) > 0 ;
}

function isCanView(){
    global $db; // database handler
    
    $this_role = isset($_SESSION['role'][$_GET['subbagian']]) ? $_SESSION['role'][$_GET['subbagian']] : null;
    if( isLogged() && $this_role != null ){
        $q = "SELECT `view` FROM `caramel_managerial`.`role_detail` WHERE `name`='$this_role'";
        $r = $db->get_var($q);
        if( $r == 'ya' ){
            return true;
        }
        else{
            return false;
        }
    }
    
    return false;
}


function isCanEntry(){
    global $db; // database handler
    
    $this_role = isset($_SESSION['role'][$_GET['subbagian']]) ? $_SESSION['role'][$_GET['subbagian']] : null;
    if( isLogged() && $this_role != null ){
        $q = "SELECT `entry` FROM `caramel_managerial`.`role_detail` WHERE `name`='$this_role'";
        $r = $db->get_var($q);
        if( $r == 'ya' ){
            return true;
        }
        else{
            return false;
        }
    }
    
    return false;
}


function isCanDelete(){
    global $db; // database handler
    
    $this_role = isset($_SESSION['role'][$_GET['subbagian']]) ? $_SESSION['role'][$_GET['subbagian']] : null;
    if( isLogged() && $this_role != null ){
        $q = "SELECT `delete` FROM `caramel_managerial`.`role_detail` WHERE `name`='$this_role'";
        $r = $db->get_var($q);
        if( $r == 'ya' ){
            return true;
        }
        else{
            return false;
        }
    }
    
    return false;
}

function isCanEdit(){
    global $db; // database handler
    
    $this_role = isset($_SESSION['role'][$_GET['subbagian']]) ? $_SESSION['role'][$_GET['subbagian']] : null;
    if( isLogged() && $this_role != null ){
        $q = "SELECT `edit` FROM `caramel_managerial`.`role_detail` WHERE `name`='$this_role'";
        $r = $db->get_var($q);
        if( $r == 'ya' ){
            return true;
        }
        else{
            return false;
        }
    }
    
    return false;
}

function clearEditSessionState(){
    if(isset($_SESSION['edit_id'])){
        unset($_SESSION['edit_id']);
    }
}
?>